package com.ruoyi.eservice.service.impl;

import java.util.List;
import java.util.Arrays;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.base.BaseService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.eservice.mapper.ShoppingCartMapper;
import com.ruoyi.eservice.pojo.entity.ShoppingCart;
import com.ruoyi.eservice.service.IShoppingCartService;

/**
 * 购物车Service业务层处理
 *
 * @author Jackson
 * @date 2022-11-11
 */
@Service
public class ShoppingCartServiceImpl extends BaseService implements IShoppingCartService
{
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    /**
     * 查询购物车
     *
     * @param shoppingCartId 购物车主键
     * @return 购物车
     */
    @Override
    public ShoppingCart selectShoppingCartByShoppingCartId(Long shoppingCartId)
    {
        return shoppingCartMapper.selectById(shoppingCartId);
    }

    /**
     * 查询购物车列表
     *
     * @param shoppingCart 购物车
     * @return 购物车
     */
    @Override
    public List<ShoppingCart> selectShoppingCartList(ShoppingCart shoppingCart)
    {
        QueryWrapper<ShoppingCart> queryWrapper = createSelWrapper(ShoppingCart.class);
        queryWrapper.lambda()
                .eq(StringUtils.isNotNull(shoppingCart.getShoppingCartId()), ShoppingCart::getShoppingCartId, shoppingCart.getShoppingCartId())
                .like(StringUtils.isNotEmpty(shoppingCart.getName()), ShoppingCart::getName, shoppingCart.getName())
                .eq(StringUtils.isNotEmpty(shoppingCart.getImage()), ShoppingCart::getImage, shoppingCart.getImage())
                .eq(StringUtils.isNotNull(shoppingCart.getUserId()), ShoppingCart::getUserId, shoppingCart.getUserId())
                .eq(StringUtils.isNotNull(shoppingCart.getDishId()), ShoppingCart::getDishId, shoppingCart.getDishId())
                .eq(StringUtils.isNotNull(shoppingCart.getSetmealId()), ShoppingCart::getSetmealId, shoppingCart.getSetmealId())
                .eq(StringUtils.isNotEmpty(shoppingCart.getDishFlavor()), ShoppingCart::getDishFlavor, shoppingCart.getDishFlavor())
                .eq(StringUtils.isNotNull(shoppingCart.getNumber()), ShoppingCart::getNumber, shoppingCart.getNumber())
                .eq(StringUtils.isNotNull(shoppingCart.getAmount()), ShoppingCart::getAmount, shoppingCart.getAmount())
        ;
        return shoppingCartMapper.selectList(queryWrapper);
    }

    /**
     * 新增购物车
     *
     * @param shoppingCart 购物车
     * @return 结果
     */
    @Override
    public int insertShoppingCart(ShoppingCart shoppingCart)
    {
        shoppingCart.setCreateBy(SecurityUtils.getUsername());
        shoppingCart.setCreateTime(DateUtils.getNowDate());
        shoppingCart.setUpdateBy(SecurityUtils.getUsername());
        shoppingCart.setUpdateTime(DateUtils.getNowDate());
        int rows = shoppingCartMapper.insert(shoppingCart);
        return rows;
    }

    /**
     * 修改购物车
     *
     * @param shoppingCart 购物车
     * @return 结果
     */
    @Override
    public int updateShoppingCart(ShoppingCart shoppingCart)
    {
        shoppingCart.setUpdateBy(SecurityUtils.getUsername());
        shoppingCart.setUpdateTime(DateUtils.getNowDate());

        return shoppingCartMapper.updateById(shoppingCart);
    }

    /**
     * 批量删除购物车
     *
     * @param shoppingCartIds 需要删除的购物车主键
     * @return 结果
     */
    @Override
    public int deleteShoppingCartByShoppingCartIds(Long[] shoppingCartIds)
    {
        return shoppingCartMapper.deleteBatchIds(Arrays.asList(shoppingCartIds));
    }

    /**
     * 删除购物车信息
     *
     * @param shoppingCartId 购物车主键
     * @return 结果
     */
    @Override
    public int deleteShoppingCartByShoppingCartId(Long shoppingCartId)
    {
        return shoppingCartMapper.deleteById(shoppingCartId);
    }
}
